package com.my.chapter11;

import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;

public class practice30 {
    public static void main(String[] args) {
        CollectionSequence c = new CollectionSequence();
        InterfaceVsIterator.display(c);
        InterfaceVsIterator.display(c.iterator());
    }
}

class Pets{
    int id;
    String name;
    static Pets[] createArray(int length){
        Pets[] pets = new Pets[length];
        for(int i=0;i<length;i++){
            pets[i] = new Pets(i,"Pet"+i);
        }
        return pets;
    }
    public Pets(int id,String name){
        this.id = id;
        this.name= name;
    }

    @Override
    public String toString() {
        return name;
    }
}

class CollectionSequence implements Collection<Pets> {

    private Pets[] pets = Pets.createArray(8);

    @Override
    public int size() {
        return pets.length;
    }

    @Override
    public boolean isEmpty() {
        return (this.size() == 0) ? true : false;
    }

    @Override
    public boolean contains(Object o) {
        return false;
    }

    @Override
    public Iterator<Pets> iterator() {

        return new Iterator<Pets>() {

            private int index = 0;

            public boolean hasNext() {
                return index < pets.length;
            }

            @Override
            public Pets next() {
                return pets[index++];
            }

            public void remove() { // Not implemented
                throw new UnsupportedOperationException();
            }
        };
    }


            @Override
    public Object[] toArray() {
        return pets;
    }

    @Override
    public <T> T[] toArray(T[] a) {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean add(Pets pets) {
        return false;
    }

    @Override
    public boolean remove(Object o) {
        return false;
    }

    @Override
    public boolean containsAll(Collection<?> c) {
        return false;
    }

    @Override
    public boolean addAll(Collection<? extends Pets> c) {
        return false;
    }

    @Override
    public boolean removeAll(Collection<?> c) {
        return false;
    }

    @Override
    public boolean retainAll(Collection<?> c) {
        return false;
    }

    @Override
    public void clear() {

    }
}

class InterfaceVsIterator{
    public static void display(Iterator<Pets> it){
        while(it.hasNext()){
            Pets p = it.next();
            System.out.print(p.id + ":" + p + " ");
        }
        System.out.println("");
    }

    public static void display(Collection<Pets> pets){
        for(Pets p:pets){
            System.out.print(p.id + ":" + p + " ");
        }
        System.out.println("");
    }
}
